#include "check.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>

using namespace std;

/* default constructor for feature_lib class */
feature_lib::feature_lib() {
	feature_lib::size = 0;
	feature_lib::points = new feature_ele[LIB_SIZE];
}

/* customized constructor for feature_lib class */
feature_lib::feature_lib(int s) {
	feature_lib::size = 0;
	feature_lib::points = new feature_ele[s];
}

/* destructor for feature_lib class
feature_lib::~feature_lib() {
	size = 0;
	cout<<"feature_lib::delete points"<<endl;
	delete []points;
}
*/
/* print each of the element in the lib */
void feature_lib::print_ele() {
	cout<<"power\t\tlocation\t"<<endl;
	for(int i = 0; i<size; i++) {
		cout<<get_power(points[i])<<"\t"<<get_location(points[i])<<"\t"<<endl;
	}
}

/* add one element to the library */
bool feature_lib::add_ele(struct feature_ele ele) {
	if (feature_lib::size>LIB_SIZE) return false;
	else {
		int location = get_location(ele);
		double power = get_power(ele);
		feature_ele *temp = new feature_ele();
		set_location(temp, location);
		set_power(temp, power);
		feature_lib::points[size] = *temp;
		delete temp;
		feature_lib::size ++;
		return true;
	}
}

/* double check if the element is in the library, in case bloom filter negative falt
 return the power of matching elem in library, else return -1 */
double feature_lib::double_check(const struct feature_ele ele) {
	int lib_size = feature_lib::size;
	for( int i = 0; i< lib_size; i++ ) {
		if(get_location(feature_lib::points[i]) == get_location(ele)) {
			printf("Find the matched location %d with power %f\n", get_location(points[i]), get_power(points[i]));
			return get_power(feature_lib::points[i]);
		}
	}
	return -1;
}

/* check if the power are within the limit*/
bool check_power(const struct feature_ele input, double orig_pow){
	double div = get_power(input) / orig_pow;
	if( div > CHK_POW_UP) {
		printf("The power of input is bigger than %f times of original\n", CHK_POW_UP);
		return false;
	}
	else if( div < CHK_POW_LOW ) {
		printf("The power of input is smaller than %f times of original\n", CHK_POW_LOW);
		return false;
	}
	else {
		return true;
	}
}

/* return the power of the element */
double get_power(const struct feature_ele ele) {
	return ele.power;
}

/* return the location of the element */
int get_location(const struct feature_ele ele) {
	return ele.location;
}

/* set the power of the element */
void set_power(struct feature_ele *ele, double pow) {
	(*ele).power = pow;
}

/* set the location of the element */
void set_location(struct feature_ele *ele, int loc) {
	(*ele).location = loc;
}

/* get the feature_ele from file and set the feature_lib */
void get_lib_from_file(char *file_name, feature_lib *lib) {
	char line[1024];
	double power;
	int location;
	feature_ele ele;

	ifstream ifs(file_name);
	if(ifs) {
		while(ifs.getline(line, sizeof(line))) {
			sscanf(line, "%lf\t%d", &power, &location);
			set_location(&ele, location);
			set_power(&ele, power);
			(*lib).add_ele(ele);
		}
	}
	ifs.close();
}

/* write the feature_lib into file */
void set_lib_to_file(char *file_name, feature_lib lib) {
	int location;
	double power;
	ofstream ofs;
	int lib_size = lib.get_size();

	ofs.open(file_name, ios_base::app);
	for( int i = 0; i < lib_size; i++ ) {	
		location = get_location(lib.get_index_ele(i));
		power = get_power(lib.get_index_ele(i));
	
		ofs << power << '\t' << location << endl;
	}
	ofs.close();
	
}

/* write the feature_lib into file 
void set_lib_to_file(char *file_name, feature_lib lib) {
	int lib_size = lib.get_size();
	for( int i = 0; i < lib_size; i++ ) {
		set_ele_to_file(file_name, lib.get_index_ele(i));
	}
}
*/
